package com.kobeliu.controller;

import com.kobeliu.enums.ArticleStatus;
import com.kobeliu.pojo.bo.EditArticleBO;
import com.kobeliu.pojo.vo.ArticleInfoVO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.service.ArticleService;
import com.kobeliu.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/save")
    public GraceJSONResult save(@RequestBody EditArticleBO bo){

        if(bo == null || StringUtils.isBlank(bo.getTitle()) || StringUtils.isBlank(bo.getContent())){
            return GraceJSONResult.errorMsg("参数错误，请重试");
        }

        articleService.save(bo);
        return GraceJSONResult.ok();
    }

    @PostMapping("/list")
    public GraceJSONResult list(Integer page,
                                Integer limit){

        PagedGridResult result = articleService.list(null,page,limit);
        return GraceJSONResult.ok(result);
    }

    @PostMapping("/detail")
    public GraceJSONResult detail(String articleId){
        if(Objects.equals(articleId, "undefined") || StringUtils.isBlank(articleId)){
            return GraceJSONResult.errorMsg("传入的数据有问题");
        }
        ArticleInfoVO vo = articleService.detail(articleId,null);
        return GraceJSONResult.ok(vo);
    }

    @PostMapping("/delete")
    public GraceJSONResult deleteById(String articleId){
        if(Objects.equals(articleId, "undefined") || StringUtils.isBlank(articleId)){
            return GraceJSONResult.errorMsg("传入的数据有问题");
        }
        articleService.deleteById(articleId);
        return GraceJSONResult.ok();
    }

    @PostMapping("/open")
    public GraceJSONResult openById(String articleId){
        if(Objects.equals(articleId, "undefined") || StringUtils.isBlank(articleId)){
            return GraceJSONResult.errorMsg("传入的数据有问题");
        }
        articleService.openById(articleId);
        return GraceJSONResult.ok();
    }

    //============================ app用户端使用 START================================
    @PostMapping("/app/list")
    public GraceJSONResult appList(Integer page,Integer limit){

        if(page == null) page=1;
        if(limit == null) limit=10;

        PagedGridResult result = articleService.list(ArticleStatus.OPEN,page,limit);
        return GraceJSONResult.ok(result);
    }

//    app/read?userId=1819750741745848321&articleId=1551421886526935041

    @PostMapping("/app/detail")
    public GraceJSONResult appDetail(String articleId){

        ArticleInfoVO vo = articleService.detail(articleId,ArticleStatus.OPEN);
        return GraceJSONResult.ok(vo);
    }

    @PostMapping("/app/read")
    public GraceJSONResult appRead(String userId,String articleId){
        //涉及到 累加累计 放到redis

        articleService.appRead(userId,articleId);
        return GraceJSONResult.ok();
    }


    //============================ app用户端使用 END==================================
}
